package com.example.yiparkapplication.tool;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.yiparkapplication.R;

public class CityDialog extends Dialog {

    private Context context;
    private Resources res;
    private String selectedProveince;
    private String[] provinces, cities;
    private int currentProvince;
    private int currentCity;
    private String province;
    private ListView lv_city, lv_province;
    private CityDialog.ClickListenerInterface clickListenerInterface;
    private ArrayAdapter adapter;

    public CityDialog(@NonNull Context context, String province){
        super(context, R.style.Dialog);
        this.context = context;
        res = context.getResources();
        selectedProveince = province;
    }

    @Override
    protected void onCreate(Bundle saveInstancedState){
        super.onCreate(saveInstancedState);
        init();
    }

    private void init(){
        provinces = res.getStringArray(R.array.province);
        String[] s = selectedProveince.split(",");
        if (!"，地球".equals(selectedProveince) && s.length == 2){
            for (int i=0;i<provinces.length;i++){
                if (s[0].equals(provinces[i])){
                    currentProvince = i;
                    break;
                }
            }

            cities = getCities(currentProvince - 1);

            for (int i=0;i<cities.length;i++){
                if (s[1].equals(cities[i])){
                    currentCity = i;
                    break;
                }
            }

            province = s[0];
        }else{
            province = "北京市";
        }

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.fragment_chose_my_location_layout, null);
        setContentView(view);

        lv_province = (ListView) view.findViewById(R.id.lv_dialog1_1);
        lv_city = (ListView) view.findViewById(R.id.lv_dialog1_2);
        TextView bt1 = (TextView) view.findViewById(R.id.bt_dialog1);
        bt1.setOnClickListener(new clickListener());
        TextView bt2 = (TextView) view.findViewById(R.id.bt_dialog2);
        bt2.setOnClickListener(new clickListener());

        /**
         * 给列表设置样式，加载内容数据
         * 并且通过setSelection设置当前默认选中德省份和城市名*/
        adapter = new ArrayAdapter<String>(context, R.layout.fragment_list_item_layout, provinces);
        lv_province.setAdapter(adapter);
        lv_province.setSelection(currentProvince - 1);

        adapter = new ArrayAdapter<String>(context, R.layout.fragment_list_item_layout, cities);
        lv_city.setAdapter(adapter);
        lv_city.setSelection(currentCity - 1);

        lv_province.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                if (scrollState == SCROLL_STATE_IDLE){
                    int position = lv_province.getFirstVisiblePosition();
                    province = provinces[position + 1];
                    lv_province.setSelection(position);
                    cities = getCities(position);
                    adapter = new ArrayAdapter<String>(context, R.layout.fragment_list_item_layout, cities);
                    lv_city.setAdapter(adapter);
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstvisibleItem, int visibleItemCount, int TotalTiemCount){}
        });
        lv_province.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                province = provinces[position];
                lv_province.setSelection(position - 1);
                cities = getCities(position - 1);
                adapter = new ArrayAdapter<String>(context, R.layout.fragment_list_item_layout, cities);
                lv_city.setAdapter(adapter);
            }
        });

        lv_city.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                if (scrollState == SCROLL_STATE_IDLE){
                    int position = lv_city.getFirstVisiblePosition();
                    lv_city.setSelection(position);
                }
            }
            @Override
            public void onScroll(AbsListView view, int intfirstVisibleItem, int visibleItemCount, int totalItemCount){}
        });
        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                if (cities[position] != null && !"".equals(cities[position])){
                    lv_city.setSelection(position - 1);
                }
            }
        });
    }

    private String[] getCities(int position){
        switch (position){
            case 0 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.北京市)[1];
                break;
            case 1 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.天津市)[1];
                break;
            case 2 :
                cities = res.getStringArray(R.array.河北省);
                break;
            case 3 :
                cities = res.getStringArray(R.array.山西省);
                break;
            case 4 :
                cities = res.getStringArray(R.array.内蒙古自治区);
                break;
            case 5 :
                cities = res.getStringArray(R.array.辽宁省);
                break;
            case 6 :
                cities = res.getStringArray(R.array.吉林省);
                break;
            case 7 :
                cities = res.getStringArray(R.array.黑龙江省);
                break;
            case 8 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.上海市)[1];
                break;
            case 9 :
                cities = res.getStringArray(R.array.江苏省);
                break;
            case 10 :
                cities = res.getStringArray(R.array.浙江省);
                break;
            case 11 :
                cities = res.getStringArray(R.array.安徽省);
                break;
            case 12 :
                cities = res.getStringArray(R.array.福建省);
                break;
            case 13 :
                cities = res.getStringArray(R.array.江西省);
                break;
            case 14 :
                cities = res.getStringArray(R.array.山东省);
                break;
            case 15 :
                cities = res.getStringArray(R.array.河南省);
                break;
            case 16 :
                cities = res.getStringArray(R.array.湖北省);
                break;
            case 17 :
                cities = res.getStringArray(R.array.湖南省);
                break;
            case 18 :
                cities = res.getStringArray(R.array.广东省);
                break;
            case 19 :
                cities = res.getStringArray(R.array.广西壮族自治区);
                break;
            case 20 :
                cities = res.getStringArray(R.array.海南省);
                break;
            case 21 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.重庆市)[1];
                break;
            case 22 :
                cities = res.getStringArray(R.array.四川省);
                break;
            case 23 :
                cities = res.getStringArray(R.array.贵州省);
                break;
            case 24 :
                cities = res.getStringArray(R.array.云南省);
                break;
            case 25 :
                cities = res.getStringArray(R.array.西藏自治区);
                break;
            case 26 :
                cities = res.getStringArray(R.array.陕西省);
                break;
            case 27 :
                cities = res.getStringArray(R.array.甘肃省);
                break;
            case 28 :
                cities = res.getStringArray(R.array.青海省);
                break;
            case 29 :
                cities = res.getStringArray(R.array.宁夏回族自治区);
                break;
            case 30 :
                cities = res.getStringArray(R.array.新疆维吾尔自治区);
                break;
            case 31 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.台湾省)[1];
                break;
            case 32 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.香港特别行政区)[1];
                break;
            case 33 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.澳门特别行政区)[1];
                break;
            case 34 :
                cities = new String[2];
                cities[1] = res.getStringArray(R.array.海外)[1];
                break;
        }
        return cities;
    }

    private class clickListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            switch (v.getId()){
                case R.id.bt_dialog1:
                    clickListenerInterface.doConfirm();
                    break;
                case R.id.bt_dialog2:
                    clickListenerInterface.doCancel();
                    break;
                default:
                    break;
            }
        }
    }

    public void setClicklistener(CityDialog.ClickListenerInterface clickListenerInterface){
        this.clickListenerInterface = clickListenerInterface;
    }

    public interface ClickListenerInterface{
        void doConfirm();
        void doCancel();
    }

    public String getProvince(){
        return province;
    }

    public String getCity(){
        return cities[lv_city.getFirstVisiblePosition()+1];
    }

}
